package com.siwei.mes.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.dto.SupplierCompanyDto;
import com.siwei.mes.entity.system.SupplierCompany;
import com.siwei.mes.entity.system.Warning;
import com.siwei.mes.enums.WarningEnum;
import com.siwei.mes.mapper.materials.SupplierCompanyMapper;
import com.siwei.mes.mapper.system.WarningMapper;
import com.siwei.mes.service.system.SupplierCompanyService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 供应商(SupplierCompany)表服务实现类
 *
 * @author linzi
 * @since 2024-07-16 16:40:40
 */
@Service
public class SupplierCompanyServiceImpl implements SupplierCompanyService {
    @Resource
    private SupplierCompanyMapper supplierCompanyMapper;
    @Resource
    private WarningMapper warningMapper;

    @Override
    public Long create(SupplierCompany entity) {
        entity.init(true);
        assertTrue(supplierCompanyMapper.selectBySupplierName(entity.getSupplierName()) != null, "该供应商已存在");
        supplierCompanyMapper.insertSelective(entity);
        return entity.getId();
    }

    /***
     * 这里改造了  用erp的yclcompany做的主表   这个id就是erp表的ycid
     * @param entity
     */
    @Override
    public void update(SupplierCompany entity) {
        SupplierCompany supplierCompany = supplierCompanyMapper.selectByPrimaryKey(entity.getId());
        if (supplierCompany == null) {
            entity.init(true);
            entity.setSupplierName(entity.getErpCompanyName());
            entity.setErpId(entity.getErpCompanyId());
            supplierCompanyMapper.insertSelective(entity);
        } else {
            entity.init(false);
            entity.setSupplierName(entity.getErpCompanyName());
            supplierCompanyMapper.updateByPrimaryKeySelective(entity);
        }
    }

    @Override
    public void delete(Long id) {
        SupplierCompany supplierCompany = supplierCompanyMapper.selectByPrimaryKey(id);
        assertTrue(supplierCompany == null, "供应商信息不存在");
        supplierCompanyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<SupplierCompany> getPage(PageQuery<SupplierCompany, SupplierCompany> query) {
        SupplierCompany searchParam = query.getParams();
        Warning warning = warningMapper.selectByWarningCode(WarningEnum.SUPPLIER.getCode());
        if (warning != null && warning.getWarningTime() > 0) {
            searchParam.setWarningDay(warning.getWarningTime());
        }
        PageInfo<SupplierCompany> pageInfo = PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> supplierCompanyMapper.getList(searchParam));
        for (SupplierCompany company : pageInfo.getList()) {
            company.setId(company.getErpCompanyId());
        }
        return pageInfo;
    }

    @Override
    public SupplierCompany loadById(Long id) {
        return supplierCompanyMapper.loadById(id);
    }

    @Override
    public SupplierCompany loadByYCId(Long ycId) {
        return supplierCompanyMapper.loadByYcId(ycId);
    }

    @Override
    public List<SupplierCompany> getAll(SupplierCompany entity) {
        return supplierCompanyMapper.getList(entity);
    }

    @Override
    public void updateStatus(SupplierCompany entity) {
        SupplierCompany supplierCompany = supplierCompanyMapper.selectByPrimaryKey(entity.getId());
        assertTrue(supplierCompany == null, "供应商信息不存在");
        entity.init(false);
        supplierCompanyMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public List<SupplierCompany> selectBySupplierCompanyList(String materialsName, String materialsSpec) {
        return supplierCompanyMapper.selectBySupplierCompanyList(materialsName, materialsSpec);
    }

    @Override
    public List<SupplierCompany> selectByMaterialSupplierCompanyList(Integer materialsType,String materialsName, String materialsSpec) {
        return supplierCompanyMapper.selectByMaterialSupplierCompanyList(materialsType,materialsName, materialsSpec);
    }

    @Override
    public List<SupplierCompanyDto> selectBySupplierCompanyListByType(Integer materialsType,String materialsName) {
        return supplierCompanyMapper.selectBySupplierCompanyListByType(materialsType,materialsName);
    }

    @Override
    public SupplierCompany saveOrUpdate(Long erpId, String companyName) {
        /**这里直接根据erpId做为主键id查询  因为erpId就是品控的主键id*/
        SupplierCompany supplierCompany = supplierCompanyMapper.selectByPrimaryKey(erpId);
        if (supplierCompany != null) {
            supplierCompany.setSupplierName(companyName);
            supplierCompany.setUpdateTime(new Date());
            supplierCompanyMapper.updateByPrimaryKeySelective(supplierCompany);
        }
        return supplierCompany;
        //SupplierCompany supplierCompany = supplierCompanyMapper.selectBySupplierName(companyName);
        ///**如果这里没有供应商需要新增一个*/
        //if (supplierCompany == null) {
        //    SupplierCompany insertInfo = new SupplierCompany();
        //    insertInfo.syncInit(true);
        //    insertInfo.setSupplierName(companyName);
        //    insertInfo.setErpId(erpId);
        //    supplierCompanyMapper.insertSelective(insertInfo);
        //    return insertInfo;
        //} else {
        //    supplierCompany.setSupplierName(companyName);
        //    supplierCompany.setErpId(erpId);
        //    supplierCompany.syncInit(false);
        //    supplierCompanyMapper.updateByPrimaryKeySelective(supplierCompany);
        //    return supplierCompany;
        //}
    }
}
